<?php

namespace app\models;

use yii\db\ActiveRecord;

/**
 * 代理商绑定Imei
 * Class AgentPublish
 * @package app\models
 *
 * @property integer $id PK
 * @property integer $agent_id 代理商编号
 * @property string $iccid iccid
 * @property integer $type 类型
 * @property string $value 内容
 * @property integer $sort 排序
 */
class BindImei extends ActiveRecord
{
    /**
     * @var string 用户类型 agent user
     */
    public $user_type;
    /**
     * @var string 用户名
     */
    public $username;
    /**
     * 关联代理商
     * @return \yii\db\ActiveQuery
     */
    public function getAgent()
    {
        return $this->hasOne(Agent::class, ['id' => 'agent_id']);
    }

    /**
     * 根据id 及类型查 名字
     * @return boolean
     */
    public function nameget($type='agent',$id){
        //echo "type:".$type;echo "<br>";
        //echo "id:".$id;echo "<br>";

        if($type=='user'){
            $user = User::findOne(['id' => $id, 'status' => User::STATUS_OK]);
        }else{
            $user = Agent::findOne(['id' => $id, 'status' => Agent::STATUS_OK]);
        }
        //pr($user);
        return $user->username;
    }
    /**
     * 根据名字及类型查id
     * @return boolean
     */
    public function useridget($type='agent',$name){
        if($type=='user'){
            $user = User::findOne(['username' => $name, 'status' => User::STATUS_OK]);
        }else{
            $user = Agent::findOne(['username' => $name, 'status' => Agent::STATUS_OK]);
        }
        return $user->id;
    }

}
